/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import DBController.UserDB;
import model.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 *
 * @author brlnt
 */
public class LoginServlet extends HttpServlet {

    private String email, password;
    UserDB userdb;
    UserBean dataUser = new UserBean();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        if (userPath.equals("/login")) {
            if (session.getAttribute("sesiUser") == null) {
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/front/login.jsp");
                rd.forward(request, response);
            } else {
                UserBean ub = (UserBean) session.getAttribute("sesiUser");
                if (!ub.isLogin()) {
                    RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/front/login.jsp");
                    rd.forward(request, response);
                } else {
                    request.setAttribute("title", "Home");
                    response.sendRedirect("./home");
                }
            }
        } else if(userPath.equals("/logout")){
            session.removeAttribute("sesiUser");
            response.sendRedirect("./welcome");
             //RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/front/welcome.jsp");
             //rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="expand" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // pembuatan session
        HttpSession session = request.getSession();
        // pembuatan objek
        userdb = new UserDB();
        email = request.getParameter("inputEmail");
        password = request.getParameter("inputPassword");

        try {
            // pengecekan login
            if (userdb.cekLogin(email, password)) {
                dataUser = userdb.getUserByEmail(email);
                dataUser.setStatusLogin(true);
                session.setAttribute("sesiUser", dataUser);
                request.setAttribute("title", "Home");
                response.sendRedirect("./home");
            } else { // autentikasi gagal
                response.sendRedirect("./login");
            }
        } catch (ClassNotFoundException e) {
            response.sendRedirect("./login");
        } catch (SQLException e) {
            response.sendRedirect("./login");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
